import type { FrontProcessing, MeshEdgeData } from "../index";
import { LineActor, PointerActor, LabelActor, BActor } from "cax-base";
export class MeasurementComp {
  engine: FrontProcessing;

  pointer1: PointerActor | null = null;
  pointer2: PointerActor | null = null;
  p2pLine: LineActor | null = null;

  measureActors: BActor[] = [];
  coverMeasureActor: LineActor | null = null;

  constructor(engine: FrontProcessing) {
    this.engine = engine;
  }

  PickPointOne(x: number, y: number, z: number, id: number) {
    this.pointer1 = new PointerActor([x, y, z], { color: [1, 0, 0] });
    this.pointer1.createdBy = "MeasurementComp";
    this.pointer1.userdata.id = id;
    this.measureActors.push(this.pointer1);
    this.engine.model.add(this.pointer1);
  }

  PickPointTwo(x: number, y: number, z: number, id: number) {
    this.pointer2 = new PointerActor([x, y, z], { color: [1, 0, 0] });
    this.pointer2.createdBy = "MeasurementComp";
    this.pointer2.userdata.id = id;
    this.measureActors.push(this.pointer2);
    this.engine.model.add(this.pointer2);
  }

  createP2PLine() {
    if (this.pointer1 && this.pointer2) {
      this.p2pLine = new LineActor(
        [...this.pointer1?.center],
        [...this.pointer2?.center]
      );
      console.log("this.p2pLine", this.pointer1.position);
      this.measureActors.push(this.p2pLine);
      this.engine.model.add(this.p2pLine);
    }
  }
  coverMeshLine(
    data: MeshEdgeData,
    start: [number, number, number],
    end: [number, number, number],
    color: [number, number, number],
    lineWidth: number
  ) {
    // 1. 移除旧线段（如果存在）
    if (this.coverMeasureActor) {
      this.engine.model.remove(this.coverMeasureActor);
      this.coverMeasureActor = null; // 明确释放引用
    }
    // 2. 创建并添加新线段
    this.coverMeasureActor = new LineActor(start, end, data, {
      color,
      lineWidth,
    });
    this.engine.model.add(this.coverMeasureActor);
  }
  createMeshLine(
    data: MeshEdgeData,
    start: [number, number, number],
    end: [number, number, number],
    color: [number, number, number],
    lineWidth: number
  ) {
    this.p2pLine = new LineActor(start, end, data, {
      color,
      lineWidth,
    });
    this.measureActors.push(this.p2pLine);
    this.engine.model.add(this.p2pLine);
    console.log("this.measureActors", this.measureActors);
  }
  removeMeshActor(uid: string) {
    console.log("this.measureActors", this.measureActors);
    this.measureActors.forEach((actor, index) => {
      if (actor instanceof LineActor && uid === actor.meshEdgeData?.uid) {
        this.engine.model.remove(actor);
        this.measureActors.splice(index, 1);
      }
    });
  }

  createDistanceLabel(metadata: any) {
    const distance = metadata[0].distance;
    if (this.pointer1 != null && this.pointer2 != null) {
      const [x1, y1, z1] = this.pointer1.center;
      const [x2, y2, z2] = this.pointer2.center;
      const labelActor = new LabelActor(
        [(x1 + x2) / 2, (y1 + y2) / 2, (z1 + z2) / 2],
        {
          text: distance.toFixed(2).toString(),
        }
      );
      this.measureActors.push(labelActor);
      this.engine.model.add(labelActor);
    }
  }

  // 清除测距结果
  clearMeasureActors() {
    this.measureActors.forEach((actor) => {
      this.engine.model.remove(actor);
    });
    this.measureActors.length = 0;
  }

  pointToPoint(curPtr: PointerActor) {
    if (this.pointer1 && this.pointer2) {
      this.pointer1 = null;
      this.pointer2 = null;
    }
    if (!this.pointer1) {
      this.PickPointOne(
        curPtr.position[0],
        curPtr.position[1],
        curPtr.position[2],
        curPtr.userdata.id
      );
      return;
    }
    if (!this.pointer2) {
      this.PickPointTwo(
        curPtr.position[0],
        curPtr.position[1],
        curPtr.position[2],
        curPtr.userdata.id
      );
      this.createP2PLine(); // 这一步直接把line添加上去了。
      if (this.pointer1 != null && this.pointer2 != null) {
        const id1 = this.pointer1.userdata.id;
        const id2 = (this.pointer2 as PointerActor).userdata.id;
        this.engine.measure.p2p(id1, id2, 1, 1).then((metadata: any) => {
          this.createDistanceLabel(metadata);
        });
      }
      return;
    }
  }
}
